
// pages/myAddress/myAddress.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectAddressId: '',
    addressList: [],
    default_id: '',
    setDefaultFlag: false,
    isFromOrder: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.frompage == 'order') {
      this.setData({ isFromOrder: true });
    }
  },
  getAddressList: function() {
    let that = this;
    app.get('User/getuseraddress', {}, function (res) {
      if (res.code == 200 && res.data.length > 0) {
        var default_id = '';
        for(var i=0; i<res.data.length; i++) {
          var address = res.data[i];
          if (address.is_default == '1') {
            default_id = address.address_id;
          }
        }
        that.setData({
          addressList: res.data,
          default_id: default_id
        });
      }
    })
  },
  addAddress: function () {
    wx.navigateTo({ url: '../myAddressEdit/myAddressEdit' });
  },
  editAddress: function(e) {
    let item = e.currentTarget.dataset.item;
    var url = 'city=' + item.province + ',' + item.city + ',' + item.area + '&mobile=' 
            + item.mobile + '&username=' + item.username + '&address_detail=' + item.address_detail 
            + '&address_id=' + item.address_id;
    wx.navigateTo({
      url: '../myAddressEdit/myAddressEdit?' + url,
    })
  },
  deleteAddress: function (e) {
    let that = this;
    app.get('User/useraddressaction', {
      address_id: e.currentTarget.dataset.id,
      type: '3' //1:新增 2:修改 3:删除
    }, function (res) {
      if (res.code == 200) {
        wx.showToast({
          title: res.message,
          duration: 1000,
          success: function () {
            wx.navigateTo({
              url: '../myAddress/myAddress',
            })
          }
        })
      }
    })
  },
  defaultFun: function(e) {
    var item = e.currentTarget.dataset.item;
    this.setData({
      default_id: item.address_id,
      setDefaultFlag: true
    })
  },
  setDefault: function(e) {
    let that = this;
    app.get('User/setDefaultAddress', {
      defaultAddress: that.data.default_id
    }, function (res) {
      wx.showToast({
        title: res.message,
      });
      wx.navigateBack({
        delta: 1
      })
    })
  },
  selectAddress: function() {
    if (this.data.isFromOrder == true) {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({ selectAddressId: this.data.default_id });
    }
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})